<?php

namespace apiend\modules\user\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * 用户收藏的商品列表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FavoriteProductSearch extends \apiend\models\searchs\ProductSearch {

    public $queryCacheDuration = false;

    /**
     * @var integer 用户UID
     */
    public $user_id;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id'], 'required'],
            [['user_id'], 'integer'],
        ];
    }

    public function buildQuery() {
        $query = static::findActive('t', static::STATUS_ACTIVE);
        $query->innerJoinWith('collects c')->andWhere(['c.user_id' => $this->user_id]);
        $query->orderBy([
            'c.updated_at' => SORT_DESC,
        ]);
        return $query;
    }

    public function fields() {
        return [
            'id',
            'name',
            'cover' => 'thumb',
            'price',
            'link',
        ];
    }

}
